[Setup]
AppName=vztools
AppId = Weeze.Script.Tools
AppVersion=1.0.0.1
WizardStyle=modern
DefaultDirName={autopf}\vztools
DefaultGroupName=Weeze
UninstallDisplayIcon={app}\vztools.exe
Compression=lzma2
SolidCompression=yes
OutputDir=..\bin\Setup

[Languages]
Name: "chinesesimplified"; MessagesFile: "ChineseSimplified.isl"

[Files]
Source: "..\bin\Release\publish\*"; DestDir: "{app}"

[Icons]
Name: "{group}\vztools"; Filename: "{app}\vztools.exe"

[Code]
var
  AddToPathCheckbox: TNewCheckBox;

procedure InitializeWizard();
begin
  // 创建复选框
  AddToPathCheckbox := TNewCheckBox.Create(WizardForm);
  AddToPathCheckbox.Parent := WizardForm.SelectDirPage;
  AddToPathCheckbox.Top := WizardForm.DirEdit.Top + WizardForm.DirEdit.Height + 8;
  AddToPathCheckbox.Left := WizardForm.DirEdit.Left;
  AddToPathCheckbox.Width := WizardForm.DirEdit.Width;
  AddToPathCheckbox.Caption := '将程序安装目录添加到系统环境变量中';
end;

function AddToPathChecked: Boolean;
begin
  Result := AddToPathCheckbox.Checked;
end;

procedure AddToEnvPath(const VarName, VarValue: string);
var
  PathValue: string;
  NewPathValue: string;
begin
  if not RegValueExists(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', VarName) then
  begin
    RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', VarName, VarValue);
  end;
  // 读取当前的 PATH 环境变量
  if RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', PathValue) then
  begin
    // 检查 PATH 中是否已经包含该值
    if Pos(';%' + VarName + '%;', ';' + PathValue + ';') = 0 then
    begin
      // 如果不包含，则添加该值
      if PathValue = '' then
        NewPathValue := '%' + VarName + '%'
      else
        NewPathValue := PathValue + ';' + '%' + VarName + '%';

      // 更新 PATH 环境变量
      RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', NewPathValue);
    end;
  end;
end;

procedure RemoveFromEnvPath(const ValueToRemove: string);
var
  PathValue: string;
  NewPathValue: string;
  Index: Integer;
begin
  // 读取当前的 PATH 环境变量
  if RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', PathValue) then
  begin
    NewPathValue := '';
    Index := Pos(';%' + ValueToRemove + '%;', ';' + PathValue + ';');
    while Index > 0 do
    begin
      // 拼接不包含要移除值的部分
      NewPathValue := NewPathValue + Copy(';' + PathValue + ';', 1, Index - 1);
      Delete(PathValue, 1, Index + Length('%'+ValueToRemove + '%'));
      Index := Pos(';' + ValueToRemove + ';', ';' + PathValue + ';');
    end;
    NewPathValue := NewPathValue + PathValue;

    // 去除首尾多余的分号
    if (Length(NewPathValue) > 0) and (NewPathValue[1] = ';') then
      Delete(NewPathValue, 1, 1);
    if (Length(NewPathValue) > 0) and (NewPathValue[Length(NewPathValue)] = ';') then
      Delete(NewPathValue, Length(NewPathValue), 1);

    // 更新 PATH 环境变量
    RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', NewPathValue);
    RegDeleteValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', ValueToRemove);
  end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if (CurStep = ssInstall) and AddToPathChecked then
  begin
    // 将 %VZTOOLS_PATH% 添加到 PATH 环境变量
    AddToEnvPath('VZTOOLS_PATH', ExpandConstant('{app}'));
  end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if (CurUninstallStep = usUninstall) then
  begin
    // 从 PATH 环境变量中移除 %VZTOOLS_PATH%
    RemoveFromEnvPath('VZTOOLS_PATH');
  end;
end;